package com.director.struts;

import com.director.core.DirectConfiguration;
import com.director.core.Provider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;

/**
 * Author: Simone Ricciardi
 * Date: 30-mag-2010
 */
public class DirectRouterAction implements Action, ServletRequestAware {

   private static final Log LOG = LogFactory.getLog(DirectRouterAction.class);

   private DirectConfiguration configuration;
   private String formattedOutput;
   private HttpServletRequest request;

   @Inject(required = true)
   public void seDirectConfiguration(DirectConfiguration directConfig) {
      this.configuration = directConfig;
   }

   public String getFormattedOutput() {
      return formattedOutput;
   }

   @Override
   public void setServletRequest(HttpServletRequest request) {
      this.request = request;
   }

   public String execute() throws Exception {
      String providerId = request.getParameter(this.configuration.getProviderParamName());
      if(providerId != null) {
         Provider provider = this.configuration.getProvider(providerId);
         this.formattedOutput = provider.process(request);
      } else {
         this.formattedOutput = this.configuration.getFormattedApi();
      }
      return SUCCESS;
   }
}
